<?php

// 完整的JWT认证测试脚本
use Illuminate\Foundation\Application;
use Tymon\JWTAuth\Providers\LaravelServiceProvider;
use Illuminate\Auth\AuthServiceProvider;
use Illuminate\Config\Repository;
use Illuminate\Events\Dispatcher;
use Tymon\JWTAuth\JWTGuard;
use Illuminate\Http\Request;
use Dotenv\Dotenv;

// 捕获错误并显示详细信息
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// 注册自动加载器
require __DIR__.'/vendor/autoload.php';

// 加载.env文件
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

// 创建应用实例
$app = new Application(dirname(__DIR__));

// 绑定配置
$app->bind('config', function () {
    // 使用Laravel的配置仓库
    $repository = new Repository();
    
    // 加载配置文件
    $authConfig = require __DIR__.'/config/auth.php';
    $jwtConfig = require __DIR__.'/config/jwt.php';
    
    // 确保JWT密钥已设置
    $jwtConfig['secret'] = env('JWT_SECRET');
    
    $repository->set('auth', $authConfig);
    $repository->set('jwt', $jwtConfig);
    
    return $repository;
});

// 绑定请求
$app->bind('request', function () {
    return Request::createFromGlobals();
});

// 绑定事件分发器
$app->bind('events', function () {
    return new Dispatcher();
});

// 注册基础服务提供者
$authProvider = new AuthServiceProvider($app);
$authProvider->register();

// 注册JWT服务提供者
$jwtProvider = new LaravelServiceProvider($app);
$jwtProvider->register();
$jwtProvider->boot(); // 确保boot方法被调用

try {
    echo "测试JWT认证服务...\n";
    
    // 检查是否已经注册了jwt服务
    echo "检查JWT服务...\n";
    if ($app->bound('tymon.jwt')) {
        echo "JWT服务已注册\n";
    } else {
        echo "JWT服务未注册，手动注册...\n";
        // 手动创建JWT实例
        $manager = $app->make('tymon.jwt.manager');
        $app->instance('tymon.jwt', $manager->getJWTProvider());
    }
    
    // 检查是否已经扩展了jwt guard
    echo "\n检查auth扩展...\n";
    $auth = $app['auth'];
    
    // 手动扩展auth guard
    $auth->extend('jwt', function ($app, $name, array $config) {
        echo "扩展jwt guard...\n";
        $guard = new JWTGuard(
            $app['tymon.jwt'],
            $app['auth']->createUserProvider($config['provider']),
            $app['request']
        );

        $app->refresh('request', $guard, 'setRequest');
        return $guard;
    });
    
    // 现在尝试获取API guard
    echo "\n获取API guard...\n";
    $apiGuard = $auth->guard('api');
    echo "API guard获取成功\n";
    
    // 显示guard的类名
    echo "guard类名: ".get_class($apiGuard)."\n";
    
    // 检查是否有attempt方法
    echo "是否有attempt方法: ".(method_exists($apiGuard, 'attempt') ? '是' : '否')."\n";
    
    // 检查是否有factory方法
    echo "是否有factory方法: ".(method_exists($apiGuard, 'factory') ? '是' : '否')."\n";
    
    // 如果有factory方法，尝试调用
    if (method_exists($apiGuard, 'factory')) {
        echo "\n测试factory()方法...\n";
        try {
            $factory = $apiGuard->factory();
            echo "factory()方法调用成功\n";
            echo "factory类名: ".get_class($factory)."\n";
            
            // 检查是否有getTTL方法
            if (method_exists($factory, 'getTTL')) {
                echo "\n测试getTTL()方法...\n";
                $ttl = $factory->getTTL();
                echo "getTTL()返回值: ".$ttl."\n";
            } else {
                echo "factory没有getTTL方法\n";
            }
        } catch (Exception $e) {
            echo "调用factory()方法出错: ".$e->getMessage()."\n";
            echo "错误堆栈: ".$e->getTraceAsString()."\n";
        }
    }
    
    // 显示所有可用的方法
    echo "\nGuard可用方法: ";
    $methods = get_class_methods($apiGuard);
    echo implode(', ', $methods)."\n";
    
} catch (Exception $e) {
    echo "错误: ".$e->getMessage()."\n";
    echo "错误堆栈: ".$e->getTraceAsString()."\n";
}